/*
 * @Author: chyang0822 270917365@qq.com
 * @Date: 2024-10-25 17:30:28
 * @LastEditors: chyang0822 270917365@qq.com
 * @LastEditTime: 2024-10-28 09:29:51
 * @FilePath: /Project-DMAI-Deploy/tutorials/concurrency/listing_2.2.cpp
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
#include <thread>

void do_something(int& i)
{
    ++i;
}

struct func
{
    int& i;

    func(int& i_):i(i_){}

    void operator()()
    {
        for(unsigned j=0;j<1000000;++j)
        {
            do_something(i);
        }
    }
};

void do_something_in_current_thread()
{}

void f()
{
    int some_local_state=0;
    func my_func(some_local_state);
    std::thread t(my_func);
    try
    {
        do_something_in_current_thread();
    }
    catch(...)
    {
        t.join(); //等待线程完成
        throw;
    }
    t.join();
}

int main()
{
    f();
}
